<template>
  <div class="ele-body">
    <el-card shadow="never">
      <el-form
          :model="where"
          inline size="mini"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
        <el-form-item label="所属学校:">
          <el-select
              filterable
              clearable
              v-model="where.organization"
              @change="getSceneList"
              size="small"
              placeholder="-请选择所属学校-"
              class="ele-block">
            <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="所属场地:">
          <el-select
              filterable
              clearable
              v-model="where.scene_id"
              @change="$forceUpdate()"
              size="small"
              placeholder="-请选择所属场地-"
              class="ele-block">
            <el-option v-for="item in sceneList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload">查询
          </el-button>
          <el-button size="mini" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columns"
          :selection.sync="selection"
          height="calc(100vh - 315px)">
        <template slot="toolbar">
          <el-button
              size="mini"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit(null)"
              v-if="permission.includes('sys:equip:add')">添加
          </el-button>
          <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:equip:dall')">删除
          </el-button>
        </template>
        <template slot="action" slot-scope="{row}">
          <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              @click="openEdit(row)">修改
          </el-link>
          <el-popconfirm
              class="ele-action"
              title="确定要删除此广告机吗？"
              @confirm="remove(row)">
            <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-delete">删除
            </el-link>
          </el-popconfirm>
        </template>
        <template slot="online" slot-scope="{row}">
          <el-tag v-if="row.online === 0" type="danger" size="small">离线</el-tag>
          <el-tag v-else type="success" size="small">在线</el-tag>
        </template>
      </ele-pro-table>
    </el-card>
    <edit v-if="showEdit" :data="current" :visible.sync="showEdit" @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import Edit from './edit'

export default {
  components: {Edit},
  computed: {
    ...mapGetters(['permission']),
  },
  data() {
    return {
      url: '/ad_equip/index',
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'organization_name',
          label: '所属学校',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'scene_name',
          label: '所属场地',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'name',
          label: '广告机名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'online',
          label: '在线状态',
          align: 'center',
          width: 100,
          slot: 'online',
        },
        {
          prop: 'code',
          label: '设备编码',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'account',
          label: '账号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'password',
          label: '密码',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'sort',
          label: '显示顺序',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'note',
          label: '备注',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue)
          }
        },
        {
          prop: 'device_update',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue)
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      where: {},
      selection: [],
      current: null,
      showEdit: false,
      schoolList: [],
      sceneList: [],
    };
  },
  mounted() {
    this.getSchoolList()
  },
  methods: {
    reload() {
      this.$refs.table.reload({where: this.where})
    },
    reset() {
      this.where = {}
      this.reload()
    },
    openEdit(row) {
      this.current = row
      this.showEdit = true
    },
    remove(row) {
      const loading = this.$loading({lock: true})
      this.$http.post('/ad_equip/delete', {id: row.id}).then(res => {
        loading.close()
        if (res.data.code === 0) {
          this.$message.success(res.data.msg)
          this.reload()
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(e => {
        loading.close()
        this.$message.error(e.message)
      })
    },
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的电子班牌吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true})
        this.$http.post('/ad_equip/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close()
          if (res.data.code === 0) {
            this.$message.success(res.data.msg)
            this.reload()
          } else {
            this.$message.error(res.data.msg)
          }
        }).catch(e => {
          loading.close()
          this.$message.error(e.message)
        });
      }).catch(() => {
      })
    },
    getSchoolList() {
      if (this.where.organization) {
        this.where.scene_id = null
        this.$forceUpdate()
        this.getSceneList()
      }
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    getSceneList() {
      if (this.where.scene_id) {
        this.where.scene_id = null
        this.$forceUpdate()
      }
      this.$http.post('/sitescene/getSceneList', {oid: this.where.organization}).then(res => {
        if (res.data.code === 0) {
          this.sceneList = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
  }
}
</script>

<style scoped>
</style>
